my $inFileName = "C:/Users/Mittal/Documents/Work/Data/Adolescent/Test/testAim2_bbr_mice_imputed_0.txt";
my $outFileName = '> C:/Users/Mittal/Documents/Work/Data/Adolescent/Test/testAim2_csv_mice_imputed_0.txt';
open(IN, $inFileName);
open (OUT, $outFileName);
my $count = 1;
my %hVars;
my $maxCol = 1;
#filling hash
while(<IN>) {
    chomp;
    @t = split " ";
    @t1  = split ":",$t[0];
    my ($i,$j);
    
    for ($i = 1; $i < @t; $i++)
    {
	@t1  = split ":",$t[$i];
	$col = int($t1[0]);
	if(!exists($hVars{$col})){
	    $hVars{$col} = $maxCol;
	    $maxCol++;
	}
    }
}
print "Number of unique vars = " . keys(%hVars);
close(IN);

open(IN, $inFileName);

my @aKeys = sort {$a <=> $b} keys(%hVars);
while(<IN>) {
    chomp;
    @t = split " ";
    @t1  = split ":",$t[0];
    
    my $max = (sort { $b <=> $a } @aKeys)[0];
    my @aVars = (0) x ($max-1);
    

    my $line;
#    $line = $line . "$count";
    $count++;
    
    $line = $line . "$t1[0]";
    
    for ($i = 1; $i < @t; $i++)
    {
	@t1  = split ":",$t[$i];
	$col = int($t1[0]);
	if($col >= 2){
	    $aVars[$col-2] = $t1[1];
	}
    }
    my $aLine = join(',', @aVars);
    if(length($aLine) > 0)
    {
        $line = $line . ",$aLine";
    }
    print OUT "$line\n";
}
close(OUT);
close(IN);